<?php
namespace app\front\controller;

use app\front\logic\CommonLogic;
use app\front\logic\ApiLogic;
use app\front\logic\MemberGradeLogic;
use app\front\logic\ActivityLogic;
use app\front\logic\MemberFromLogic;
use app\front\logic\RechargeOrderLogic;
use app\common\logic\AliPayLogic;
use think\facade\Db;

use app\common\logic\MemberAccountLogic;
use app\common\logic\RechargeOrderLogic as CommonRechargeOrderLogic;
use app\common\logic\MemberLevelLogic;

//账户充值
class Recharge extends MemberBase{
	//提交充值信息,生成充值订单
	function toRecharge(){
		//开始生成订单
		$amount = input('amount', 0);
		$number = 0;		//LTC和USDT的数量
		$pay_type = input('pay_type', 10);
		if(!is_numeric($amount) || $amount<=0){
			return ApiLogic::return($this->lang, 137);
		}

		//获取会员等级折扣
		$grade = $this->getGradeDiscount();
		//获取充值活动折扣
		$activity = $this->getActivityDistinct();
		//获取首次充值返现折扣
		$firstRecharge = $this->getFirstDistinct();

		if($pay_type==20){
			//LTC，先转换成当前货币
			$number = $amount;
			$amount = $amount * $this->ltc_rate;
		}elseif($pay_type==30){
			//USDT，先转换成当前货币
			$number = $amount;
			$amount = $amount * $this->usdt_rate;
		}elseif($pay_type==10){
			//支付宝支付，这边不需要转换金额了
		}else{
			return ApiLogic::return($this->lang, 138);
		}

		//将amount转化成人民币
		$amount = $amount * $this->exchange_rate;

		// echo $amount;

		//存储订单信息
		$data = [];
		$data['member_id'] 		= $this->member_info['id'];
		$data['recharge_sn'] 	= CommonLogic::init()->createSn('order_recharge','recharge_sn',14,'number','RO');
		$data['trade_number']	= $data['recharge_sn']."_".rand(1000,9999);
		$data['pay_type'] 		= $pay_type;
		$data['status'] 		= 10;
		$data['create_time'] 	= date('Y-m-d H:i:s');
		$data['number'] = $number;

		//总金额
		$data['recharge_amount'] = $amount;

		//默认设置为0
		$data['level_amount'] = 0;
		$data['first_amount'] = 0;
		$data['gift_amount'] = 0;
		$data['gift_amount'] = 0;
		$data['origin_invitation_amount'] = 0;
		$data['origin_member_id'] = 0;

		//具有等会返现
		if($grade && isset($grade['gift_rate'])){
			$data['level_amount'] = $amount * $grade['gift_rate'] / 100;
		}

		//具有首单返现
		if($firstRecharge){
			$data['first_amount'] = $amount * $firstRecharge['member_rate'] / 100;
			$data['origin_member_id'] = $firstRecharge['origin_member_id'];
			$data['origin_invitation_amount'] = $amount * $firstRecharge['origin_rate'] / 100;;
		}

		//具有活动返现
		if($activity && is_array($activity)){
			foreach($activity as $k=>$v){
				if($amount>=$v['amount']){
					$data['gift_amount'] = $amount * $v['gift_rate'] / 100;
					break;
				}
			}
		}
		$rs = RechargeOrderLogic::init()->add($data);
		
		//订单生成成功，去支付
		if($pay_type==10){
			$prs = AliPayLogic::init()->webPay($data);
		}elseif($pay_type==20){
			$prs = "LTC支付地址";
		}else{
			$prs = "USDT支付地址";
		}
		return ApiLogic::return($this->lang, $rs,'',['pay_address'=>$prs,'recharge_sn'=>$data['recharge_sn']]);
	}

	//查询是否支付成功了
	function queryRecharge(){
		$recharge_sn = input('recharge_sn', '');
		if(!$recharge_sn){
			return ApiLogic::return($this->lang, 155);
		}

		$info = Db::name('order_recharge')->where('recharge_sn',$recharge_sn)->find();
		if(!$info){
			return ApiLogic::return($this->lang, 149);
		}

		if($info['status']==20){
			$pdata = [];
			$pdata['status'] = 1;
			$pdata['msg'] = "支付成功，金额已到账";
			return ApiLogic::return($this->lang, 0, '订单支付成功', $pdata);
		}

		if($info['status']==30 || $info['status']==40){
			return ApiLogic::return($this->lang, 156);
		}
		// print_r($info);exit;
		//调用支付宝接口，查询
		$rst = AliPayLogic::init()->queryPay($recharge_sn);
		if(!$rst){
			$pdata = [];
			$pdata['status'] = 0;
			$pdata['msg'] = "等待支付中";
			return ApiLogic::return($this->lang, 0, '', $pdata);
		}elseif($rst->trade_status=="WAIT_BUYER_PAY"){
			$pdata = [];
			$pdata['status'] = 0;
			$pdata['msg'] = "等待支付中";
			return ApiLogic::return($this->lang, 0, '', $pdata);
		}elseif($rst->trade_status=="TRADE_SUCCESS"){
			$pdata = [];
			$pdata['status'] = 1;
			$pdata['msg'] = "支付成功，金额已到账";
		}

		//订单支付完成了，改变订单状态，金额充值到账户
		Db::startTrans();
		try {
			//开始充值
	    	$rs = MemberAccountLogic::init()->accountIn($info);
	    	// echo $rs.'A1';
	    	if(!$rs){
	    		Db::rollback();
	    		return ApiLogic::return($this->lang, 158);
	    	}
	    	//处理充值订单
			$rs2 = CommonRechargeOrderLogic::init()->orderProcess($info,20,$rst->trade_no, json_encode($rst));
	    	// echo $rs2.'A2';
	    	if(!$rs2){
	    		Db::rollback();
	    		return ApiLogic::return($this->lang, 158);
	    	}
	    	//充值金额写入这个余额的充值等级信息中
			$rs3 = MemberLevelLogic::init()->levelProcess($info['member_id'], $info['recharge_amount']);
			if(!$rs3){
	    		Db::rollback();
	    		return ApiLogic::return($this->lang, 158);
	    	}
	    	
			Db::commit();
			return ApiLogic::return($this->lang, 0, '',$pdata);
		} catch (\Exception $e) {
			// print_r($e->getMessage());
		    // 回滚事务
		    Db::rollback();
		    return ApiLogic::return($this->lang, 158);
		}
		
	}

	//获取充值优惠信息
	function getDiscount(){
		//获取会员等级折扣
		$grade = $this->getGradeDiscount();
		//获取充值活动折扣
		$activity = $this->getActivityDistinct();
		//获取首次充值返现折扣
		$firstRecharge = $this->getFirstDistinct();

		$data = [];
		$data['grade'] = $grade;
		$data['activity'] = $activity;
		$data['firstRecharge'] = $firstRecharge;
		return ApiLogic::return($this->lang,0,'',$data);
	}
	//会员等级折扣
	function getGradeDiscount(){
		$info = MemberGradeLogic::init()->getGradeInfo($this->lang, $this->member_info['grade_level']);
		return $info;
	}
	//活动折扣
	function getActivityDistinct(){
		$list = ActivityLogic::init()->getActivityDistinct($this->lang, $this->member_info['grade_level']);
		if($this->lang=='en'){
			if($list){
				foreach($list as $k=>$v){
					$list[$k]['amount'] = $v['amount'] / $this->exchange_rate;
				}
			}
		}
		return $list;
	}
	//第一次充值折扣
	function getFirstDistinct(){
		$rate = MemberFromLogic::init()->getFirstDistinct($this->member_id);
		return $rate;
	}

	//获取充值订单
	function getOrderList(){
		$rs = RechargeOrderLogic::init()->getOrderList($this->member_id, $this->exchange_rate);
		return ApiLogic::return($this->lang, 0, '', $rs);
	}

	
}